﻿using UnityEngine;
using GameFramework;
using UnityGameFramework.Runtime;
using System.Collections.Generic;

namespace MYSTARFORCE
{
    /// <summary>
    /// 飞机抽象类逻辑
    /// </summary>
    public abstract class Aircraft : TargetableObject
    {
        [SerializeField]
        private AircraftData m_AircreaftData = null;

        //TODO: 推进器、盔甲、武器待制作

        protected List<Weapon> m_Weapons = new List<Weapon>();



        protected override void OnShow(object userData)
        {
            base.OnShow(userData);
            m_AircreaftData = (AircraftData)userData;

            if(m_AircreaftData == null)
            {
                Log.Error("Aircraft data is invalid");
                return;
            }
            // 修改战机的名字为AircraftXXX
            Name = string.Format("Aircraft {0}", Id.ToString());

            //TODO:给战机添加推进器
            //TODO:给战机添加武器列表
            List<WeaponData> weaponDatas = m_AircreaftData.GetAllWeaponDatas();
            for (int i = 0; i < weaponDatas.Count; i++)
            {
                GameEntry.Entity.ShowWeapon(weaponDatas[i]);
            }
            //TODO:给战机添加盔甲列表

        }

        protected override void OnHide(object userData)
        {
            base.OnHide(userData);
        }

        protected override void OnAttached(EntityLogic childEntity, Transform parentTransform, object userData)
        {
            base.OnAttached(childEntity, parentTransform, userData);
            //TODO: 给战机实体添加子实体-推进器
            //TODO: 给战机实体添加子实体-武器
            if(childEntity is Weapon)
            {
                m_Weapons.Add((Weapon)childEntity);
                Log.Warning("m_Weapons.add()");
                return;
            }
            //TODO: 给战机实体添加子实体-盔甲
        }

        protected override void OnDetached(EntityLogic childEntity, object userData)
        {
            base.OnDetached(childEntity, userData);

            //TODO: 解除战机的子实体-推进器
            //TODO: 解除战机的子实体-武器
            if(childEntity is Weapon)
            {
                m_Weapons.Remove((Weapon)childEntity);
                return;
            }
            //TODO: 解除战机的子实体-盔甲
        }

        /// <summary>
        /// 当实体死亡时
        /// </summary>
        /// <param name="attacker">攻击者</param>
        protected override void OnDead(Entity attacker)
        {
            base.OnDead(attacker);

            // 播放死亡时的特效
            EffectData deadEffect = new EffectData(GameEntry.Entity.GenerateSerialId(), m_AircreaftData.DeadEffectId)
            {
                Position = CachedTransform.localPosition
            };
            GameEntry.Entity.ShowEffect(deadEffect);

            // 播放死亡时的音效
            GameEntry.Sound.PlaySound(m_AircreaftData.DeadSoundId);
        }

        
        public override ImpactData GetImpactData()
        {
            return new ImpactData(m_AircreaftData.Camp, m_AircreaftData.HP, 0, m_AircreaftData.Defence);
        }
    }
}